//项目路径
var strPath = window.document.location.pathname;
//var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var postPath = "/gdp";
var dtConfigId = GetQueryString("dtConfigId");
var operateMethod = GetQueryString("operateMethod");

var jsonDataList = JSON.parse(decode_utf8(GetQueryString("jsonDataList")));
console.log("luowen dataList", jsonDataList)



$(document).ready(function () {
    //加载数据

    if(isAddMode()){
        $("#tb_fieldList").bootstrapTable({
            url: postPath + '/system/dataConfig/getNewFieldList',
            undefinedText: '-',
            striped: true, // 是否显示行间隔色
            queryParams: {dtConfigId: dtConfigId},
            uniqueId: "key", // 每一行的唯一标识
            columns: [{
                field: 'state',
                checkbox: true,
                align: 'center',
                valign: 'middle'
            }, {
                title: '字段名称',
                field: 'dfName',
                align: 'center',
                valign: 'middle'
            }, {
                title: '中文名称',
                field: 'dfNameCn',
                align: 'center',
                valign: 'middle',
                editable: true
            }, {
                title: '字段类型',
                field: 'dtType',
                align: 'center',
                valign: 'middle'
            },{
                title: '备注',
                field: 'remark',
                align: 'center',
                valign: 'middle',
                editable: true
            }],

        });

    }else {

        $("#tb_fieldList").bootstrapTable({
            data: jsonDataList,
            undefinedText: '-',
            striped: true, // 是否显示行间隔色
            uniqueId: "key", // 每一行的唯一标识
            columns: [{
                field: 'state',
                checkbox: true,
                align: 'center',
                valign: 'middle'
            }, {
                title: '字段名称',
                field: 'dfName',
                align: 'center',
                valign: 'middle'
            }, {
                title: '中文名称',
                field: 'dfNameCn',
                align: 'center',
                valign: 'middle',
                editable: true
            }, {
                title: '字段类型',
                field: 'dtType',
                align: 'center',
                valign: 'middle'
            },{
                title: '备注',
                field: 'remark',
                align: 'center',
                valign: 'middle',
                editable: true
            }],

        });


    }

    //新增操作
    $("#btn_fieldAdd").click(function () {

        var rowList = $('#tb_fieldList').bootstrapTable('getSelections');//多选
        if (rowList == null || rowList == "") {
            bootbox.alert("请选择一行!");
            return;
        }

        // here is change content to display
        console.log("luowen add rowList", rowList)
        var cfgFieldList = [];
        rowList.forEach(function(row, i){
            var cfgField = {
                "guid": row.guid,
                "dfName": row.dfName,
                "dfNameCn": row.dfNameCn,
                "dfOrder": row.dfOrder,
                "dtType": row.dtType,
                "remark": row.remark,
                "dtConfigId": dtConfigId
            };
            cfgFieldList.push(cfgField)
        })

        $.ajax({
            url: postPath + '/system/dataConfig/addFieldList',
            type: 'POST',
            contentType: "application/json",
            data: JSON.stringify(cfgFieldList),
            success: function (data) {
                showMsg("新增成功!","success");
                console.log("luowen addFieldList", data)

                $('#tb_fieldList').bootstrapTable('refresh');
                parent.closeModul();
            },
            error: function () {
                showMsg("新增失败,请重试!","error");
            }
        });

    });

    //修改操作
    $("#btn_fieldUpdate").click(function () {
        var rowList = $('#tb_fieldList').bootstrapTable('getSelections');//多选
        if (rowList == null || rowList == "") {
            bootbox.alert("请选择一行!");
            return;
        }

        // here is change content to display
        console.log("luowen update rowList", rowList)
        var cfgFieldList = [];
        rowList.forEach(function(row, i){
            var cfgField = {
                "guid": row.guid,
                "dfName": row.dfName,
                "dfNameCn": row.dfNameCn,
                "dfOrder": row.dfOrder,
                "dtType": row.dtType,
                "remark": row.remark,
                "dtConfigId": row.dtConfigId
            };
            cfgFieldList.push(cfgField)
        })

        $.ajax({
            url: postPath + '/system/dataConfig/updateFieldList',
            type: 'POST',
            contentType: "application/json",
            data: JSON.stringify(cfgFieldList),
            success: function (data) {
                showMsg("新增成功!","success");
                console.log("luowen addFieldList", data)

                $('#tb_fieldList').bootstrapTable('refresh');
                parent.closeModul();
            },
            error: function () {
                showMsg("新增失败,请重试!","error");
            }
        });


    });



})

function GetQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

function showMsg(text, icon) {
    $.toast().reset('all');
    $.toast({
        text: text,
        icon: icon,
        showHideTransition: 'fade',
        allowToastClose: true,
        hideAfter: 1500,
        stack: false,
        position: 'top-right',
        textAlign: 'left',
        loader: true,
        loaderBg: '#9ec600',
    });
}

function isAddMode(){
    var isAddMode = operateMethod == 'add';
    var addButton = document.getElementById("btn_fieldAdd")
    var updateButton = document.getElementById("btn_fieldUpdate")

    if(isAddMode){
        // add show update hidden
        addButton.style.display = "block";
        updateButton.style.display = "none";

    }else{
        // update show add hidden
        addButton.style.display = "none";
        updateButton.style.display = "block";
    }
    return  isAddMode;
}

function encode_utf8(s) {
  return encodeURIComponent(s);
}

function decode_utf8(s) {
  return decodeURIComponent(escape(s));
}
